package com.xrz.controller;

import com.xrz.common.Result;
import com.xrz.dao.FriendDao;
import com.xrz.dao.QaqUserDao;
import com.xrz.pojo.Friend;
import com.xrz.pojo.vo.FriendVo;
import com.xrz.pojo.vo.QaqUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Api(value = "好友controller", tags = {"好友操作接口"})
@RestController
@RequestMapping("/friends")
public class FriendsController {


    @Resource
    FriendDao friendDao;
    @Resource
    QaqUserDao qaqUserDao;
    //获取我的好友
    @ApiOperation(value = "获取我的好友")
    @GetMapping("/getMyFriends/{userId}")
    public Result<?> getMyFriends(@PathVariable Integer userId) {
        Collection<FriendVo> friends = friendDao.getMyFriends(userId);
        return Result.success(friends);
    }
    //用户名搜索用户
    @ApiOperation(value = "用户名精确搜索用户")
    @GetMapping("/getUserByName/{username}")
    public Result<?> getUserByUsername(@PathVariable String username) {
        if(username==null){
            return Result.error(0,"请输入用户名");
        }
        QaqUserVo qaqUsers= qaqUserDao.getUserByUsername(username);
        if (qaqUsers==null){
            return Result.error(0,"用户不存在");
        }
        return Result.success(qaqUsers);
    }
    //手机号搜索用户
    @ApiOperation(value = "手机号精确搜索用户")
    @GetMapping("/getUserByPhone/{phone}")
    public Result<?> getUserByPhone(@PathVariable String phone) {
        if (phone==null){
            return Result.error(0,"请输入手机号");
        }
        QaqUserVo qaqUsers= qaqUserDao.getUserInfoByPhone(phone);
        if (qaqUsers==null){
            return Result.error(0,"用户不存在");
        }
        return Result.success(qaqUsers);
    }
    //昵称模糊搜索
    @ApiOperation(value = "昵称模糊搜索用户")
    @GetMapping("/getUserByNick/{nickName}")
    public Result<?> getUserByNick(@PathVariable String nickName) {
        if(nickName==null){
            return  Result.error(0,"请输入昵称");
        }
        Collection<QaqUserVo> qaqUsers= qaqUserDao.getUserInfoByNick(nickName);
        if (qaqUsers==null){
            return Result.error(0,"用户不存在");
        }
        return Result.success(qaqUsers);
    }
    //添加好友
    @ApiOperation(value = "添加好友")
    @GetMapping("/addFriends/{friendId}/{myId}")
    public Result<?> addFriends(@PathVariable Integer myId, @PathVariable Integer friendId) {
        if (qaqUserDao.selectByPrimaryKey(friendId)==null){
            return Result.error(0,"没有该用户");
        }

        if (friendId.equals(myId)) {
            return Result.error(0,"不能加自己为好友");
        }
        List<Friend> friends=friendDao.selectByMyId(myId);
        for (Friend friend1 : friends) {
            if (friend1.getFriendId().equals(friendId)){
                return Result.error(0,"添加失败，已是好友");
            }
        }
        Friend My=new Friend();
        My.setFriendId(friendId);
        My.setMyId(myId);
        int res = friendDao.insert(My);
        Friend friend=new Friend();
        friend.setFriendId(myId);
        friend.setMyId(friendId);
        int res1 = friendDao.insert(friend);
        if (res==1&&res1==1){
            return Result.success("添加成功");
        }else {
            return Result.error(0,"添加失败");
        }
    }

    //删除好友
    @ApiOperation(value = "删除好友")
    @GetMapping("/deleteFriends/{friendId}/{myId}")
    public Result<?> deleteFriends(@PathVariable Integer myId, @PathVariable Integer friendId) {
        int res = friendDao.deleteFriends(friendId,myId);
        if (res==1){
            return Result.success("删除成功");
        }
        return Result.error(0,"删除失败");
    }
}
